home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 10 - Networking / NovelNetwar / helpBox.c < prev    next >
Text File  |  1995-05-12  |  9KB  |  471 lines

  1. //    Show a friendly help box to the user.
  2.  
  3. #include "NovelNetwar.h"
  4.  
  5.  
  6.  
  7.  
  8. static WindowPtr        helpWPtr;
  9. static TEHandle            helpTEH;
  10. static Rect                helpDestRect,helpViewRect,helpVScrollRect;
  11. static ControlHandle    helpScrollControl;
  12. static int                helpNumLines;
  13.  
  14.  
  15. ControlActionUPP helpScrollProc(ControlHandle theControl,int theCode);
  16. static void helpResizeWindow(void);
  17.  
  18.  
  19.  
  20. void helpInit(void)
  21. {
  22.     helpWPtr = 0L;
  23.     helpTEH = 0L;
  24.     helpScrollControl = 0L;
  25. }
  26.  
  27.  
  28. void helpStartup(void)
  29. {
  30.  
  31.  
  32. }
  33.  
  34.  
  35. void helpShutDown(void)
  36. {
  37.     if (helpTEH)
  38.         TEDispose(helpTEH);
  39.     
  40.     if (helpScrollControl)
  41.         DisposeControl(helpScrollControl);
  42.     
  43.     if (helpWPtr)
  44.         DisposeWindow(helpWPtr);
  45.     
  46.     helpScrollControl = 0L;
  47.     helpWPtr = 0L;
  48.     helpTEH = 0L;
  49. }
  50.  
  51.  
  52.  
  53.  
  54. ControlActionUPP helpScrollProc(ControlHandle theControl,int theCode)
  55. {
  56. int            scrollAmt,height;
  57. int            controlMax,controlMin,controlVal;
  58.  
  59.     controlMax = GetCtlMax(theControl);
  60.     controlMin = GetCtlMin(theControl);
  61.     controlVal = GetCtlValue(theControl);
  62.     
  63.     switch (theCode) 
  64.     {
  65.         case inUpButton:
  66.             if (controlVal > controlMin)
  67.             {
  68.                 SetCtlValue(theControl,controlVal-1);
  69.                 
  70.                 scrollAmt = TEGetHeight(controlVal,controlVal,helpTEH);
  71.                 
  72.                 TEScroll(0,scrollAmt,helpTEH);
  73.             }
  74.             
  75.             break;
  76.             
  77.         case inPageUp: 
  78.             if (controlVal > controlMin)
  79.             {
  80.                 scrollAmt = 1;
  81.                 height = helpViewRect.bottom - helpViewRect.top;
  82.                 
  83.                 while (controlVal-scrollAmt>controlMin && TEGetHeight(controlVal-scrollAmt,controlVal-1,helpTEH)<height)
  84.                     scrollAmt++;
  85.                 
  86.                 if (scrollAmt>1  && TEGetHeight(controlVal-scrollAmt,controlVal-1,helpTEH)>height)
  87.                     scrollAmt--;
  88.                 
  89.                 SetCtlValue(theControl,controlVal-scrollAmt);
  90.                 
  91.                 TEScroll(0,TEGetHeight(controlVal-scrollAmt,controlVal-1,helpTEH),helpTEH);
  92.             }
  93.             
  94.             break;
  95.  
  96.         case inDownButton: 
  97.             if (controlVal < controlMax)
  98.             {
  99.                 SetCtlValue(theControl,controlVal+1);
  100.                 
  101.                 scrollAmt = -TEGetHeight(controlVal+1,controlVal+1,helpTEH);
  102.                 
  103.                 TEScroll(0,scrollAmt,helpTEH);
  104.             }
  105.             
  106.             break;
  107.  
  108.         case inPageDown: 
  109.             if (controlVal < controlMax)
  110.             {
  111.                 scrollAmt = 1;
  112.                 height = helpViewRect.bottom - helpViewRect.top;
  113.                 
  114.                 while (controlVal+scrollAmt<controlMax && TEGetHeight(controlVal+1,controlVal+scrollAmt-1+1,helpTEH)<height)
  115.                     scrollAmt++;
  116.                 
  117.                 if (scrollAmt>1  && TEGetHeight(controlVal+1,controlVal+scrollAmt-1+1,helpTEH)>height)
  118.                     scrollAmt--;
  119.                 
  120.                 SetCtlValue(theControl,controlVal+scrollAmt);
  121.                 
  122.                 TEScroll(0,-TEGetHeight(controlVal+1,controlVal+scrollAmt-1+1,helpTEH),helpTEH);
  123.             }
  124.             
  125.             break;
  126.     }
  127. }
  128.  
  129.  
  130.  
  131. void DoHelpBox(int helpResWIND,int helpResTEXT,int helpResSTYL)
  132. {
  133. Handle            helpText,helpStyle;
  134. Cursor            waitCursor;
  135. CursHandle        hCurs;
  136.     
  137.     
  138.  
  139.     if (helpWPtr)
  140.     {
  141.         SelectWindow(helpWPtr);
  142.         
  143.         return;
  144.     }
  145.     
  146.     
  147.     helpText = GetResource('TEXT',helpResTEXT);
  148.     helpStyle = GetResource('styl',helpResSTYL);
  149.     
  150.     if (helpText == 0L || helpStyle == 0L)
  151.     {
  152.         ErrorAlert("DoHelpBox: Can't load help TEXT/STYL resources!");
  153.         
  154.         helpShutDown();
  155.         
  156.         goto EXITPOINT;
  157.     }
  158.     
  159.     helpWPtr = GetNewWindow(helpResWIND,0L,(WindowPtr) -1L);
  160.     
  161.     if (helpWPtr == 0L)
  162.     {
  163.         ErrorAlert("DoHelpBox: Can't allocate helpWPtr!");
  164.         
  165.         helpShutDown();
  166.         
  167.         goto EXITPOINT;
  168.     }
  169.     
  170.     SetPort(helpWPtr);
  171.     
  172.     CenterWindow(helpWPtr);
  173.     ShowWindow(helpWPtr);
  174.     
  175.     DrawGrowIcon(helpWPtr);
  176.     
  177.     TextFont(geneva);
  178.     TextSize(10);
  179.     TextFace(0);
  180.     
  181.     SetRect(&helpViewRect,helpWPtr->portRect.left,helpWPtr->portRect.top,helpWPtr->portRect.right-15,helpWPtr->portRect.bottom-15);
  182.     InsetRect(&helpViewRect,5,5);
  183.     helpDestRect = helpViewRect;
  184.     
  185.     
  186.     helpTEH = TEStylNew(&helpDestRect,&helpViewRect);
  187.     
  188.     if (helpTEH == 0L)
  189.     {
  190.         ErrorAlert("DoHelpBox: Can't allocate helpTEH!");
  191.         
  192.         helpShutDown();
  193.         
  194.         goto EXITPOINT;
  195.     }
  196.     
  197.     (**helpTEH).lineHeight = -1;
  198.     (**helpTEH).fontAscent = -1;
  199.     
  200.     SetRect(&helpVScrollRect,helpWPtr->portRect.right-15,helpWPtr->portRect.top-1,helpWPtr->portRect.right+1,helpWPtr->portRect.bottom-14);
  201.     helpScrollControl = NewControl(helpWPtr,&helpVScrollRect,"\p",TRUE,1,1,1,scrollBarProc,(long) helpTEH);
  202.     
  203.     if (helpScrollControl == 0L)
  204.     {
  205.         ErrorAlert("DoHelpBox: Can't allocate helpScrollControl!");
  206.         
  207.         helpShutDown();
  208.         
  209.         goto EXITPOINT;
  210.     }
  211.     
  212.     SetCtlMin(helpScrollControl,0);
  213.     SetCtlValue(helpScrollControl,0);
  214.     
  215.     HLock(helpText);
  216.     
  217.     hCurs = GetCursor(watchCursor);
  218.     
  219.     
  220.     
  221.     if (hCurs)
  222.     {
  223.         waitCursor = **hCurs;
  224.     
  225.         SetCursor(&waitCursor);
  226.     }
  227.     
  228.     else
  229.     {
  230.         InitCursor();
  231.     }
  232.     
  233.     TEStylInsert(*helpText,SizeResource(helpText),(StScrpHandle) helpStyle,helpTEH);
  234.     
  235.     InitCursor();
  236.     
  237.     HUnlock(helpText);
  238.     ReleaseResource(helpText);
  239.     ReleaseResource(helpStyle);
  240.     
  241.     
  242.     helpResizeWindow();
  243.     
  244.     
  245.     BeginUpdate(helpWPtr);
  246.     EndUpdate(helpWPtr);
  247.  
  248. EXITPOINT:
  249.  
  250.     return;
  251. }
  252.  
  253.  
  254.  
  255.  
  256.  
  257. void helpCloseWindow(WindowPtr whichWindow)
  258. {
  259.     if (helpWPtr && helpWPtr == whichWindow)
  260.     {
  261.         helpShutDown();
  262.         
  263.         EnableEditMenu();
  264.     }
  265. }
  266.  
  267.  
  268. static void helpResizeWindow(void)
  269. {
  270. int        nLines,height;
  271.  
  272.     if (helpWPtr && helpScrollControl)
  273.     {
  274.         nLines = (**helpTEH).nLines;
  275.         helpNumLines = 1;
  276.         height = (**helpTEH).viewRect.bottom - (**helpTEH).viewRect.top;
  277.         
  278.         while (nLines-helpNumLines > 1 && TEGetHeight(nLines-helpNumLines,nLines-1,helpTEH) < height)
  279.             helpNumLines++;
  280. /*        
  281.         if (helpNumLines > 1  && TEGetHeight(nLines-helpNumLines,nLines-1,helpTEH) > height)
  282.             helpNumLines--;
  283. */        
  284.         if (nLines - helpNumLines > 0)
  285.             SetCtlMax(helpScrollControl,nLines - helpNumLines);
  286.         else
  287.             SetCtlMax(helpScrollControl,0);
  288.     }
  289. }
  290.  
  291.  
  292. void helpGrowWindow(WindowPtr whichWindow,EventRecord *theEvent)
  293. {
  294. Rect        tempRect;
  295. long        newSize;
  296. GrafPtr        oldPort;
  297. int            ctlVal,height;
  298.  
  299.     if (helpWPtr && whichWindow == helpWPtr && helpScrollControl)
  300.     {
  301.         SetRect(&tempRect,165,165,32767,32767);
  302.         newSize = GrowWindow(whichWindow,theEvent->where,&tempRect);
  303.         SizeWindow(whichWindow,LoWord(newSize),HiWord(newSize),0xff);
  304.         tempRect = whichWindow->portRect;
  305.         GetPort(&oldPort);
  306.         SetPort(whichWindow);
  307.         EraseRect(&tempRect);
  308.         InvalRect(&tempRect);
  309.         SetPort(oldPort);
  310.         MoveControl(helpScrollControl,tempRect.right+1-16,tempRect.top-1);
  311.         SizeControl(helpScrollControl,16,whichWindow->portRect.bottom-whichWindow->portRect.top-13);
  312.         
  313.         SetRect(&helpViewRect,helpWPtr->portRect.left,helpWPtr->portRect.top,helpWPtr->portRect.right-15,helpWPtr->portRect.bottom-15);
  314.         InsetRect(&helpViewRect,5,5);
  315.         
  316.         helpDestRect = helpViewRect;
  317.         
  318.         ctlVal = GetCtlValue(helpScrollControl);
  319.         
  320.         if (ctlVal > 0)
  321.             height = TEGetHeight((long) (ctlVal-1),0L,helpTEH);
  322.         else
  323.             height = 0;
  324.         
  325.         helpDestRect.top -= height;
  326.         
  327.         (*helpTEH)->viewRect = helpViewRect;
  328.         (*helpTEH)->destRect = helpDestRect;
  329.         
  330.         TECalText(helpTEH);
  331.         
  332.         InitCursor();
  333.         
  334.         helpResizeWindow();
  335.     }
  336. }
  337.  
  338.  
  339.  
  340.  
  341. void helpDragWindow(WindowPtr whichWindow,EventRecord *theEvent)
  342. {
  343. Rect    tempRect;
  344.     
  345.     if (helpWPtr && whichWindow == helpWPtr)
  346.     {
  347.         getGrayRgnRect(&tempRect);
  348.         DragWindow(whichWindow,theEvent->where,&tempRect);
  349.     }
  350. }
  351.  
  352.  
  353.  
  354.  
  355. void helpActivateWindow(WindowPtr whichWindow,char adFlag)
  356. {
  357. Rect            tempRect;
  358. GrafPtr            oldPort;
  359.  
  360.     if (helpWPtr && whichWindow==helpWPtr && helpScrollControl)
  361.     {
  362.         GetPort(&oldPort);
  363.         SetPort(whichWindow);
  364.         
  365.         if (adFlag)
  366.         {
  367.             HiliteControl(helpScrollControl,0);
  368.             DrawControls(whichWindow);
  369.             
  370.             DisableEditMenu();
  371.         }
  372.         else
  373.         {
  374.             HiliteControl(helpScrollControl,255);
  375.             
  376.             EnableEditMenu();
  377.         }
  378.         
  379.         DrawGrowIcon(whichWindow);
  380.         
  381.         SetPort(oldPort);
  382.     }
  383. }
  384.  
  385.  
  386.  
  387.  
  388. OSErr helpIsWindow(WindowPtr theWPtr)
  389. {
  390.     if (helpWPtr && theWPtr == helpWPtr)
  391.         return(noErr);
  392.     else
  393.         return(BADWINDOW);
  394. }
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401. void helpUpdateWindow(WindowPtr theWPtr)
  402. {
  403. GrafPtr            oldPort;
  404. Rect            tempRect;
  405.  
  406.  
  407.     if (helpWPtr && theWPtr == helpWPtr)
  408.     {
  409.         GetPort(&oldPort);
  410.         SetPort(helpWPtr);
  411.         
  412.         BeginUpdate(helpWPtr);
  413.         
  414.         tempRect = helpWPtr->portRect;
  415.         EraseRect(&tempRect);
  416.         
  417.         TEUpdate(&helpViewRect,helpTEH);
  418.         
  419.         DrawGrowIcon(helpWPtr);
  420.         DrawControls(helpWPtr);
  421.         
  422.         EndUpdate(helpWPtr);
  423.         
  424.         SetPort(oldPort);
  425.     }
  426. }
  427.  
  428.  
  429.  
  430. void helpDoContent(WindowPtr whichWindow,EventRecord *theEvent)
  431. {
  432. GrafPtr            oldPort;
  433. ControlHandle    whichControl;
  434. int                cntlCode,oldVal,controlVal;
  435.  
  436.     if (helpWPtr && whichWindow == helpWPtr && helpScrollControl)
  437.     {
  438.         GetPort(&oldPort);
  439.         SetPort(whichWindow);
  440.         
  441.         GlobalToLocal(&(theEvent->where));
  442.         
  443.         cntlCode = FindControl(theEvent->where,whichWindow,&whichControl);
  444.         
  445.         if (cntlCode != 0)
  446.         {
  447.             if (whichControl == helpScrollControl)
  448.             {
  449.                 if (cntlCode == inThumb)
  450.                 {
  451.                     oldVal = GetCtlValue(helpScrollControl);
  452.                     
  453.                     TrackControl(helpScrollControl,theEvent->where,0L);
  454.                     
  455.                     controlVal = GetCtlValue(helpScrollControl);
  456.                     
  457.                     if (controlVal > oldVal)
  458.                         TEScroll(0,-TEGetHeight(oldVal,controlVal-1,helpTEH),helpTEH);
  459.                         
  460.                     else if (controlVal < oldVal)
  461.                         TEScroll(0,TEGetHeight(controlVal,oldVal-1,helpTEH),helpTEH);
  462.                 }
  463.                 
  464.                 else
  465.                     TrackControl(whichControl,theEvent->where,(ControlActionUPP) helpScrollProc);
  466.             }
  467.         }
  468.         
  469.         SetPort(oldPort);
  470.     }
  471. }